/*
 * TestDumpCodeScp.c
 *
 *  Created on: 2012/05/30
 *      Author: guanxiaoying
 */

#include "TestDump.h"


static VOID dumpCodeScope( AST astCodeScp ){
	U4 length;

	sprintf( tempBuf, header_format, dumpGetStr( XML_RECORD ), astCodeScp );
	dumpOutputStream( tempBuf );
	length = CCodeScope.getFilePath(astCodeScp, file_buf, OS_FULLPATH );
	if( length == 0 ){
		sprintf( file_buf, "%s", dumpGetStr( XML_ERROR ) );
	}
	sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FILE ), "" );
	dumpOutputStream( tempBuf );
	dumpOutputStream( file_buf );
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FILE ) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_value_header_format , dumpGetStr(XML_LOCATION), "" );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, "%ld,%d - %ld,%d",
			CCodeScope.getBeginLine(astCodeScp),
			CCodeScope.getBeginLineOffset( astCodeScp),
			CCodeScope.getEndLine( astCodeScp),
			CCodeScope.getEndLineOffset( astCodeScp));
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_LOCATION) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, tail_format, dumpGetStr( XML_RECORD ) );
	dumpOutputStream( tempBuf );
}
VOID dumpCodeScopeAsField( AST astCodeScp ){
	U4 length;

	length = CCodeScope.getFilePath(astCodeScp, file_buf, OS_FULLPATH );
	if( length == 0 ){
		sprintf( file_buf, "%s", dumpGetStr( XML_ERROR ) );
	}
	sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FILE ), "" );
	dumpOutputStream( tempBuf );
	dumpParameterStr( file_buf );
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FILE ) );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_value_header_format , dumpGetStr(XML_LOCATION), "" );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, "%ld,%d - %ld,%d",
			CCodeScope.getBeginLine(astCodeScp),
			CCodeScope.getBeginLineOffset( astCodeScp),
			CCodeScope.getEndLine( astCodeScp),
			CCodeScope.getEndLineOffset( astCodeScp));
	dumpParameterStr( tempBuf );
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_LOCATION) );
	dumpOutputStream( tempBuf );
}
VOID DumpCodeScopeToFile( PSZ szPath, PSZ postfix ){
	AST astCodeScp;
	AST astMax;

	if( szPath == NULL ){
		return;
	}
	strcpy( file_buf, szPath );
	file_buf[strlen(szPath)] = '\0';
	strcat(file_buf, postfix );
	file_buf[strlen(szPath)+strlen(postfix)] = '\0';
	if( COEXIT_SUCCESS == dumpOutputStreamInit_file(file_buf)){
		sprintf( tempBuf, top_header, dumpGetStr(XML_CODESCOPE) );
		dumpOutputStream( tempBuf );
		astMax = CCodeScope.getMax();
		astCodeScp = MIN_ARRAY;
		while( astCodeScp <= astMax ){
			dumpCodeScope( astCodeScp );
			astCodeScp ++;
		}
		sprintf( tempBuf, top_end, dumpGetStr(XML_CODESCOPE) );
		dumpOutputStream( tempBuf );
		dumpOutputStreamEnd_file();
	}
}
